﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wq.Extensions.Common;
using Wq.Extensions.Repository;
using Wq.MeetingService.Entities;
using Wq.MeetingService.Models;
using Wq.MeetingService.Models.Enum;
using Wq.Model;

namespace Wq.MeetingService
{
    /// <summary>
    /// 会议室
    /// </summary>
    public class MeetingRoomService : SqlSugarBaseRepository<MeetingRoomInfo>, IAddSingleton
    {

        /// <summary>
        /// 验证会议室
        /// </summary>
        /// <param name="model"></param>
       
        public void Verify(MeetingRoomEdit model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
                throw new MyException("会议室名称不能为空");
            if (model.VenueId == 0)
                throw new MyException("请选择场地");
            if (model.People <= 0)
                throw new MyException("容纳人数不能为空");
            if (model.Area <= 0)
                throw new MyException("面积不能为空");
          
            if (model.Images.Count == 0)
                throw new MyException("请上传图片");
            if (base.Count(p => p.VenueId == model.VenueId) > 50 && model.Id == 0)
                throw new MyException("会议室数量不能超过50个");
        }

        /// <summary>
        /// 编辑模型转实体
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private MeetingRoomInfo EditToModel(MeetingRoomEdit info)
        {
            var model = info.Map<MeetingRoomInfo>();
            model.Layouts.ForEach(x => x.VenueId = info.VenueId);
            model.Facilities = info.FacilitieIds.Select(p => new VenueFacilitieInfo {VenueId = info.VenueId , TypeId = PositionType.Meeting, FacilitieId = p }).ToList();
            model.Thumbnails = info.Images.Select(p => new MeetingThumbnailInfo { VenueId = info.VenueId, TypeId = PositionType.Meeting, FilePath = p }).ToList();
            model.Features = info.FeaturesIds.Select(p => new VenuFeaturesInfo { VenueId = info.VenueId,TypeId = PositionType.Meeting,  FeaturesId = p }).ToList();

            return model;
        }

        /// <summary>
        /// 创建会议室
        /// </summary>
        /// <param name="info"></param>
        public void Create(MeetingRoomEdit info)
        {
            Verify(info);
            var model = EditToModel(info);
            Context.Ado.UseTran(() => {
                Context.InsertNav(model)
               .Include(p => p.Layouts)
               .Include(p => p.Facilities)
               .Include(p => p.Thumbnails)
               .Include(p => p.Features).ExecuteCommand();
                UpdateVenue(info.VenueId);
            });
            
        }

        /// <summary>
        /// 编辑会议室
        /// </summary>
        /// <param name="info"></param>
        public void Save(MeetingRoomEdit info)
        {
            Verify(info);
            var model = EditToModel(info);
            Context.Ado.UseTran(() => {
                Context.UpdateNav(model)
              .Include(p => p.Layouts)
              .Include(p => p.Facilities)
              .Include(p => p.Thumbnails)
              .Include(p => p.Features).ExecuteCommand();
                UpdateVenue(info.VenueId);
            });
           
        }

        /// <summary>
        /// 会议会议室统计字段
        /// </summary>
        /// <param name="venueId"></param>
        public void UpdateVenue(long venueId)
        {
            var count = Context.Queryable<MeetingRoomInfo>().Where(p => p.VenueId == venueId).Count();
            var minPrice = Context.Queryable<MeetingRoomInfo>().Where(p => p.VenueId == venueId && p.Price > 0).Min(p => p.Price);
            var maxPrice = Context.Queryable<MeetingRoomInfo>().Where(p => p.VenueId == venueId && p.Price > 0).Max(p => p.Price);
            var minArea = Context.Queryable<MeetingRoomInfo>().Where(p => p.VenueId == venueId && p.Area > 0).Min(p => p.Area);
            var maxArea = Context.Queryable<MeetingRoomInfo>().Where(p => p.VenueId == venueId && p.Area > 0).Max(p => p.Area);
            Context.Updateable<VenueInfo>()
                .SetColumns(p => p.MeetingRoomCount == count)
                .SetColumns(p => p.MaxMeetingRoomPrice == maxPrice)
                .SetColumns(p => p.MinMeetingRoomPrice == minPrice)
                .SetColumns(p => p.MinArea == minArea)
                .SetColumns(p => p.MaxArea == maxArea)
                .Where(p => p.Id == venueId).ExecuteCommand();
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MeetingRoomInfo Details(long id) =>
            Context.Queryable<MeetingRoomInfo>()
            .Where(p => p.Id == id)
            .Includes(p => p.Layouts)
            .Includes(p => p.Facilities)
            .Includes(p => p.Thumbnails)
            .Includes(p => p.Features)
            .First();

        /// <summary>
        /// 修改排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public void UpdateSequence(long id, long sequence) => Context.Updateable<MeetingRoomInfo>().SetColumns(p => p.Sequence == sequence).Where(p => p.Id == id).ExecuteCommand();
    }
}
